
package me.module.alert
{
	import com.bit101.components.PushButton;
	import com.bit101.components.TextArea;
	import com.bit101.components.Window;
	
	import flash.display.DisplayObjectContainer;
	import flash.display.Shape;
	import flash.display.Sprite;
	import flash.events.Event;
	
	import me.common.Common;
	
	/**
	 * @author 缺硒
	 */
	public class Alert extends Window
	{
		private static var __alert:Alert;
		
		private var _logText:TextArea;
		private var _closeBtn:PushButton;
		private var _isShow:Boolean;
		private var _modal:Sprite;
		
		
		/**
		 * 显示log
		 * @param info
		 */		
		public static function showLog(info:String):void
		{
			if(__alert == null) __alert = new Alert(new Singleton());
			__alert.showMsg(info);
		}
		
		/**
		 * 隐藏log 
		 */		
		public static function hideLog():void
		{
			if(__alert == null) __alert = new Alert(new Singleton());
			__alert.hide();
		}
		
		// 构造函数
		public function Alert(s:Singleton)
		{
			super(Common.tmain, 250, 100, "Warning");
			this.setSize(300,180);
			
			_logText = new TextArea(this, 10, 10);
			_logText.setSize(280, 100);
			_closeBtn = new PushButton(this, 90, 120, "Close", hide);
			_modal = new Sprite();
			_modal.mouseChildren = false;
			
			_closeBtn.enabled = false;
			
			hide();
		}
		
		/**
		 * 显示消息
		 * @param info 
		 */		
		public function showMsg(info:String):void
		{
			if(!_isShow) show(); 
			_logText.text += info + "\n\n";
		}
		
		//显示
		private function show():void
		{
			_modal.graphics.clear();
			_modal.graphics.beginFill(0x000000, 0.3);
			_modal.graphics.drawRect(0,0,Common.stage.nativeWindow.width, Common.stage.nativeWindow.height);
			_modal.graphics.endFill();
			
			Common.tmain.addChild(_modal);
			Common.tmain.addChild(this);
			
			this.x = (Common.stage.nativeWindow.width - this.width)/2;
			this.y = (Common.stage.nativeWindow.height - this.height)/2;
			_isShow = true;
		}
		
		//隐藏
		private function hide(evt:Event = null):void
		{
			_logText.text = "";
			if(Common.tmain.contains(this))
				Common.tmain.removeChild(this);
			if(Common.tmain.contains(_modal))
				Common.tmain.removeChild(_modal);
			_modal.graphics.clear();
			_isShow = false;
		}
		//
	}
}

class Singleton{}